/**
 * Created by Peter on 2016/5/20.
 */
define(['jquery', 'appConst', 'stringUtils'], function ($, appConst, stringUtils) {
    var data = {
        timeout: 3000
    };
    var that;


    function init(operation) {
        that = $(this);
        var defaults = {
            title: "", //
            content: "",
            type: "success", // error, warning, normal
            timeout: data.timeout,
            clean: true // set true will remove the same type box.
        };
        var opt = $.extend({}, defaults, operation);

        if (opt.clean) {
            cleanSameBox(opt.type);
        }
        if (opt.timeout == false) {
            opt.timeout = null;
        }
        $.smallBox({
            title: getTitleByType(opt.type),
            content: opt.content,
            color: getColorByType(opt.type),
            timeout: opt.timeout,
            icon: getIconByType(opt.type)
        });
    }

    function cleanSameBox(type) {
        var msg = $.i18n.prop('MESSAGEBOX_TITLE');
        if (type == "success") {
            msg = $.i18n.prop('MESSAGEBOX_TITLE_SUCCESS');
        } else if (type == "error") {
            msg = $.i18n.prop('MESSAGEBOX_TITLE_ERROR');
        } else if (type == "warning") {
            msg = $.i18n.prop('MESSAGEBOX_TITLE_WARNING');
        }
        var box = $(document).find('#divSmallBoxes').find('.SmallBox');
        $.each(box, function (i, obj) {
            if ($(obj).find(' div.textoFoto > span:contains("' + msg + '")').size() > 0) {
                $('#' + $(obj).attr('id')).remove();
            }
        });
    }

    function getColorByType(type) {
        if (type == "success") {
            return appConst.colors.success;
        } else if (type == "error") {
            return appConst.colors.danger;
        } else if (type == "warning") {
            return appConst.colors.warning;
        }

        return appConst.colors.primary;
    }

    function getIconByType(type) {
        if (type == "success") {
            return "fa fa-check";
        } else if (type == "error") {
            return "fa fa-warning shake";
        } else if (type == "warning") {
            return "fa fa-shield fadeInLeft";
        }

        return "fa fa-bell";
    }

    function getTitleByType(type) {
        if (type == "success") {
            return $.i18n.prop('MESSAGEBOX_TITLE_SUCCESS');
        } else if (type == "error") {
            return $.i18n.prop('MESSAGEBOX_TITLE_ERROR');
        } else if (type == "warning") {
            return $.i18n.prop('MESSAGEBOX_TITLE_WARNING');
        }

        return $.i18n.prop('MESSAGEBOX_TITLE');
    }

    return {
        show: function (opt) {
            init(opt);
        },
        successBox: function (opt) {
            if (stringUtils.isBlank(opt) || stringUtils.isBlank(opt.content)) {
                opt = $.extend({ content : ""}, opt);
                opt.content = $.i18n.prop('MESSAGE_CONTENT_OPT_SUCCESS');
            }
            init({
                content: opt.content,
                type: "success",
                clean: true
            });
        },
        sessionFailureBox: function () {
            init({
                content: $.i18n.prop('EXCEPTION_GLOBAL_SESSION_FAILURE')
                + "<p class='text-align-right'><a href='login' class='btn btn-primary btn-sm'>" + $.i18n.prop('MESSAGEBOX_BUTTON_LOGIN') + "</a>",
                type: "error",
                clean: true
            });
        },
        serverFailureBox: function () {
            init({
                content: $.i18n.prop('EXCEPTION_GLOBAL_SYSTEM'),
                type: "error",
                clean: true
            });
        },
        pathFailureBox: function () {
            init({
                content: $.i18n.prop('EXCEPTION_GLOBAL_PATH_FAILURE'),
                type: "error",
                clean: true
            });
        },
        serverTimeoutBox: function () {
            init({
                content: $.i18n.prop('EXCEPTION_GLOBAL_TIMEOUT'),
                type: "error",
                clean: true
            });
        },
        errorBox: function (msg, arg) {
            if (stringUtils.isBlank(msg)) {
                msg = "EXCEPTION_GLOBAL_SYSTEM";
            }
            if (stringUtils.isBlank(msg) && stringUtils.isNotBlank(arg)) {
                msg = arg;
            }

            init({
                title: $.i18n.prop('MESSAGEBOX_TITLE_ERROR'),
                content: $.i18n.prop(msg, [$.i18n.prop(arg)]),
                type: "error",
                clean: true
            });
        }
    }
});